package com.menhai.personmanagement.services;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.menhai.personmanagement.entity.FollowupRecord;
import com.menhai.personmanagement.entity.FollowupRecordCondition;
import com.menhai.personmanagement.entity.MbReportCard;
import com.menhai.personmanagement.entity.vo.MbReportCardVo;
import com.menhai.personmanagement.mapper.FollowupRecordMapper;
import com.menhai.personmanagement.mapper.ReportCardMapper;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 随访记录表 服务类
 * </p>
 *
 * @author shikun
 * @since 2021-11-25
 */
@Service
public class FollowupRecordService {

    private static final Logger logger = LoggerFactory.getLogger(FollowupRecordService.class);
    @Autowired
    private FollowupRecordMapper followupRecordMapper;
    @Autowired
    private ReportCardMapper reportCardMapper;

    /**
     * 新增随访记录
     *
     * @param Doctor
     * @return Map<String, Object>
     */
    @Transactional
    public Map<String, Object> addFollowupRecord(FollowupRecord followupRecord) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //1，新建随访记录
            int iRet = followupRecordMapper.insert(followupRecord);
            if (iRet == 0) {  //没有插入成功的数据
                resultMap.put("success", "0");
                resultMap.put("message", "插入成功的条数为0，新建随访记录失败！");
                return resultMap;
            } else {
                resultMap.put("success", "1");
                resultMap.put("message", "新建随访记录成功！");
                return resultMap;
            }
        } catch (Exception e) {
            resultMap.put("success", "0");
            resultMap.put("message", "新建随访记录失败！异常：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return resultMap;
    }

    /**
     * 编辑随访记录
     *
     * @param Doctor
     * @return Map<String, Object>
     */
    @Transactional
    public Map<String, Object> editFollowupRecord(FollowupRecord followupRecord) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //1，编辑随访记录
            int iRet = followupRecordMapper.update(followupRecord);
            if (iRet == 0) {  //没有插入成功的数据
                resultMap.put("success", "0");
                resultMap.put("message", "编辑成功的条数为0，编辑随访记录失败！");
                return resultMap;
            } else {
                resultMap.put("success", "1");
                resultMap.put("message", "编辑随访记录成功！");
                return resultMap;
            }
        } catch (Exception e) {
            resultMap.put("success", "0");
            resultMap.put("message", "编辑随访记录失败！异常：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return resultMap;
    }

    /**
     * 删除随访记录
     *
     * @param id
     * @return Map<String, Object>
     */
    @Transactional
    public Map<String, Object> deleteById(String id) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        try {
            //删除随访记录信息
            int iRet = followupRecordMapper.delete(id);
            if (iRet == 0) {
                resultMap.put("success", "0");
                resultMap.put("message", "删除成功的条数为0，删除随访记录失败！");
                return resultMap;
            }
            resultMap.put("success", "1");
            resultMap.put("message", "删除随访记录成功！");
        } catch (Exception e) {
            resultMap.put("success", "0");
            resultMap.put("message", "删除随访记录失败！异常：" + e.getMessage());
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return resultMap;
    }

    /**
     * 获取随访记录列表
     *
     * @param FollowupRecord
     * @return List<Map>
     */
    public List<Map<String, Object>> getListByCondition(FollowupRecordCondition followupRecordCondition) {
        return followupRecordMapper.getListByCondition(followupRecordCondition);
    }


    /**
     * @param hosnum
     * @param mbType
     * @describe 药品统计
     * @Return java.util.List<com.menhai.personmanagement.entity.vo.MbReportCardVo>
     * @Date 2022/2/9 15:14
     * @author shikun
     */
    public List<MbReportCardVo> drugstatistics(String hosnum, String mbType) {
        return followupRecordMapper.drugstatistics(hosnum, mbType);
    }

    public IPage<FollowupRecordCondition> getListByConditionByPage(FollowupRecordCondition followupRecordCondition, String hosnumarray) {
        Page<FollowupRecordCondition> page = new Page<>(followupRecordCondition.getPageIndex(), followupRecordCondition.getPageSize());
        List list = Arrays.stream(hosnumarray.split(",")).collect(Collectors.toList());
        return followupRecordMapper.getListByConditionByPage(page, followupRecordCondition, list);
    }

    public List gecunrenshu(String mbtype, String userid, String hosnum) {
        if (hosnum.length() < 8) {
            return followupRecordMapper.gecunrenshu(mbtype, "");
        } else {
            return followupRecordMapper.gecunrenshu(mbtype, userid);
        }
    }

    /**
     * 获取随访记录列表
     *
     * @param getOneById
     * @return List<Map>
     */
    public List<Map<String, Object>> getOneById(FollowupRecordCondition followupRecordCondition) {
        return followupRecordMapper.getOneById(followupRecordCondition);
    }

    /*
     * 从随访计划中获取患者信息
     */
    public List<Map<String, Object>> getCardNameListPlanByCondition(String patientName, String mbType, String hosnum, String shstatus, String sbstatus, String startDate, String endDate, String doctorId) {
        return followupRecordMapper.getCardNameListPlanByCondition(patientName, mbType, hosnum, shstatus, sbstatus, startDate, endDate, doctorId);
    }

    /*
     * 从报卡中获取患者信息
     */
    public List<MbReportCard> getCardNameListNoPlanByCondition(String patientName, String mbType, String hosnum, String shstatus, String sbstatus, String startDate, String endDate, String doctorId) {
        return followupRecordMapper.getCardNameListNoPlanByCondition(patientName, mbType, hosnum, shstatus, sbstatus, startDate, endDate, doctorId);
    }

    /*
     * 从报卡中获取患者信息
     */
    public List<MbReportCard> getCardNameListByCondition(String patientName, String mbType, String hosnum, String shstatus, String sbstatus, String startDate, String endDate) {
        return followupRecordMapper.getCardNameListByCondition(patientName, mbType, hosnum, shstatus, sbstatus, startDate, endDate);
    }

    /**
     * @param patientName,hosnum
     * @return
     */
    public List<Map<String, Object>> getPopulationNameListByCondition(String patientName, String hosnum) {
        return followupRecordMapper.getPopulationNameListByCondition(patientName, hosnum);
    }

    /**
     * 查询随访提醒
     *
     * @param personName
     * @return
     */
    public IPage<List<Map<String, Object>>> selectSFTX(String personName, String hosnum, String mbtype, FollowupRecordCondition followupRecordCondition) {
        Page<FollowupRecordCondition> page = new Page<>(followupRecordCondition.getPageIndex(), followupRecordCondition.getPageSize());
        return followupRecordMapper.selectSFTX(page, personName, mbtype, hosnum);
    }

    /**
     * 三天随访提醒
     *
     * @param planTime
     * @param hosnum
     * @param mbType
     * @return
     */
    public List<Map<String, Object>> getTXList(String planTime, String hosnum, String mbType, String userId, String hosnumStr, String[] hosnumArray) {
        return followupRecordMapper.getTXList(planTime, hosnum, mbType, userId, hosnumStr, hosnumArray);
    }

    public Map<String, Object> getMbsjtjhzListByYear(String queryYear, String mbType, String hosnum, String hosnumArray) {
        Map<String, Object> mbsjtjhzList = new HashMap<String, Object>();
        String[] arr = hosnumArray.split(",");

        String hosunString = Arrays.asList(arr).stream().map(s -> "'" + s + "'").collect(Collectors.joining(","));
        logger.info("============" + hosunString);
        //查询患者人数
        int hzrs = followupRecordMapper.getHzrs(queryYear, mbType, hosnum, hosunString);
        //查询登记人数
        int djrs = followupRecordMapper.getDjrs(queryYear, mbType, hosnum, hosunString);
        //登记率
        BigDecimal djl = (hzrs == 0 ? new BigDecimal(0.00 + "") : (new BigDecimal(djrs + "")).multiply(new BigDecimal("100")).divide(new BigDecimal(hzrs + ""), 2, BigDecimal.ROUND_UP));
        //规范管理人数
        int gfglrs = followupRecordMapper.getGfglrs(queryYear, mbType, hosnum, hosunString);
        //规范管理率
        BigDecimal gfgll = (djrs == 0 ? new BigDecimal(0.00 + "") : (new BigDecimal(gfglrs + "")).multiply(new BigDecimal("100")).divide(new BigDecimal(djrs + ""), 2, BigDecimal.ROUND_UP));
        //服药人数
        int fyrs = followupRecordMapper.getFyrs(queryYear, mbType, hosnum, hosunString);
        //服药率
        BigDecimal fyl = (djrs == 0 ? new BigDecimal(0.00 + "") : (new BigDecimal(fyrs + "")).multiply(new BigDecimal("100")).divide(new BigDecimal(djrs + ""), 2, BigDecimal.ROUND_UP));
        //并发症人数
        int bfzrs = followupRecordMapper.getBfzrs(queryYear, mbType, hosnum, hosunString);
        //并发率
        BigDecimal bfl = (djrs == 0 ? new BigDecimal(0.00 + "") : (new BigDecimal(bfzrs + "")).multiply(new BigDecimal("100")).divide(new BigDecimal(djrs + ""), 2, BigDecimal.ROUND_UP));
        mbsjtjhzList.put("hzrs", hzrs);
        mbsjtjhzList.put("djrs", djrs);
        mbsjtjhzList.put("djl", djl);
        mbsjtjhzList.put("gfglrs", gfglrs);
        mbsjtjhzList.put("gfgll", gfgll);
        mbsjtjhzList.put("fyrs", fyrs);
        mbsjtjhzList.put("fyl", fyl);
        mbsjtjhzList.put("bfzrs", bfzrs);
        mbsjtjhzList.put("bfl", bfl);
        return mbsjtjhzList;
    }

    public List<Map<String, Object>> getMbflrqtjbByYear(String queryYear, String hosnum) {
        List<Map<String, Object>> resultList = followupRecordMapper.getMbflrqtjbByYear(queryYear, hosnum);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        int gxynum = 0;
        int tnbnum = 0;
        int gxbnum = 0;
        int zlnum = 0;
        int jsbnum = 0;
        int othernum = 0;
        int num = 0;
        for (int i = 0; i < resultList.size(); i++) {
            gxynum = gxynum + Integer.parseInt(resultList.get(i).get("gxynum") + "");
            tnbnum = tnbnum + Integer.parseInt(resultList.get(i).get("tnbnum") + "");
            gxbnum = gxbnum + Integer.parseInt(resultList.get(i).get("gxbnum") + "");
            zlnum = zlnum + Integer.parseInt(resultList.get(i).get("zlnum") + "");
            jsbnum = jsbnum + Integer.parseInt(resultList.get(i).get("jsbnum") + "");
            othernum = othernum + Integer.parseInt(resultList.get(i).get("othernum") + "");
            num = num + Integer.parseInt(resultList.get(i).get("num") + "");
        }
        hjMap.put("gxynum", gxynum);
        hjMap.put("tnbnum", tnbnum);
        hjMap.put("gxbnum", gxbnum);
        hjMap.put("zlnum", zlnum);
        hjMap.put("jsbnum", jsbnum);
        hjMap.put("othernum", othernum);
        hjMap.put("num", num);
        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getMbsfjhtjbByDay(String queryDay, String hosnum, String userid) {
        List<Map<String, Object>> resultList = followupRecordMapper.getMbsfjhtjbByDay(queryDay, hosnum, userid);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        hjMap.put("doctorName", "");
        int gxynum = 0;
        int tnbnum = 0;
        int gxbnum = 0;
        int zlnum = 0;
        int jsbnum = 0;
        int othernum = 0;
        int num = 0;
        for (int i = 0; i < resultList.size(); i++) {
            gxynum = gxynum + Integer.parseInt(resultList.get(i).get("gxynum") + "");
            tnbnum = tnbnum + Integer.parseInt(resultList.get(i).get("tnbnum") + "");
            gxbnum = gxbnum + Integer.parseInt(resultList.get(i).get("gxbnum") + "");
            zlnum = zlnum + Integer.parseInt(resultList.get(i).get("zlnum") + "");
            jsbnum = jsbnum + Integer.parseInt(resultList.get(i).get("jsbnum") + "");
            othernum = othernum + Integer.parseInt(resultList.get(i).get("othernum") + "");
            num = num + Integer.parseInt(resultList.get(i).get("num") + "");
        }
        hjMap.put("gxynum", gxynum);
        hjMap.put("tnbnum", tnbnum);
        hjMap.put("gxbnum", gxbnum);
        hjMap.put("zlnum", zlnum);
        hjMap.put("jsbnum", jsbnum);
        hjMap.put("othernum", othernum);
        hjMap.put("num", num);
        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getMbsfgztjbByDay(String queryDay, String hosnum, String userid) {
        List<Map<String, Object>> resultList = followupRecordMapper.getMbsfgztjbByDay(queryDay, hosnum, userid);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        hjMap.put("doctorName", "");
        int gxynum = 0;
        int tnbnum = 0;
        int gxbnum = 0;
        int zlnum = 0;
        int jsbnum = 0;
        int othernum = 0;
        int num = 0;
        for (int i = 0; i < resultList.size(); i++) {
            gxynum = gxynum + Integer.parseInt(resultList.get(i).get("gxynum") + "");
            tnbnum = tnbnum + Integer.parseInt(resultList.get(i).get("tnbnum") + "");
            gxbnum = gxbnum + Integer.parseInt(resultList.get(i).get("gxbnum") + "");
            zlnum = zlnum + Integer.parseInt(resultList.get(i).get("zlnum") + "");
            jsbnum = jsbnum + Integer.parseInt(resultList.get(i).get("jsbnum") + "");
            othernum = othernum + Integer.parseInt(resultList.get(i).get("othernum") + "");
            num = num + Integer.parseInt(resultList.get(i).get("num") + "");
        }
        hjMap.put("gxynum", gxynum);
        hjMap.put("tnbnum", tnbnum);
        hjMap.put("gxbnum", gxbnum);
        hjMap.put("zlnum", zlnum);
        hjMap.put("jsbnum", jsbnum);
        hjMap.put("othernum", othernum);
        hjMap.put("num", num);
        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getMbrgzltjbByDay(String queryDay, String hosnum, String userid) {
        List<Map<String, Object>> resultList = followupRecordMapper.getMbrgzltjbByDay(queryDay, hosnum, userid);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        hjMap.put("doctorName", "");
        int jknum = 0;
        int sfnum = 0;
        int jynum = 0;
        int pgnum = 0;
        int num = 0;
        for (int i = 0; i < resultList.size(); i++) {
            int thenum = 0;
            jknum = jknum + Integer.parseInt(resultList.get(i).get("jknum") + "");
//			sfnum=sfnum+Integer.parseInt(resultList.get(i).get("sfnum")+"");
//			jynum=jynum+Integer.parseInt(resultList.get(i).get("jynum")+"");
//			pgnum=pgnum+Integer.parseInt(resultList.get(i).get("pgnum")+"");
//			thenum=Integer.parseInt(resultList.get(i).get("jknum")+"")+Integer.parseInt(resultList.get(i).get("sfnum")+"")
//			+Integer.parseInt(resultList.get(i).get("jynum")+"")+Integer.parseInt(resultList.get(i).get("pgnum")+"");
//			resultList.get(i).put("num", thenum);
//			num=num+thenum;
        }
        hjMap.put("jknum", jknum);
        hjMap.put("sfnum", sfnum);
        hjMap.put("jynum", jynum);
        hjMap.put("pgnum", pgnum);
        hjMap.put("num", num);
        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getGzlList(String queryDay, String hosnum, String userid) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> resultList = followupRecordMapper.getGzlist(beginDate, endDate, hosnum, userid);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        hjMap.put("doctorName", "");
        int jknum = 0;
        int sfnum = 0;
        int jynum = 0;
        int pgnum = 0;
        int num = 0;
        for (int i = 0; i < resultList.size(); i++) {
            int thenum = 0;
            jknum = jknum + Integer.parseInt(resultList.get(i).get("jknum") + "");
//			sfnum=sfnum+Integer.parseInt(resultList.get(i).get("sfnum")+"");
//			jynum=jynum+Integer.parseInt(resultList.get(i).get("jynum")+"");
//			pgnum=pgnum+Integer.parseInt(resultList.get(i).get("pgnum")+"");
//			thenum=Integer.parseInt(resultList.get(i).get("jknum")+"")+Integer.parseInt(resultList.get(i).get("sfnum")+"")
//			+Integer.parseInt(resultList.get(i).get("jynum")+"")+Integer.parseInt(resultList.get(i).get("pgnum")+"");
//			resultList.get(i).put("num", thenum);
//			num=num+thenum;
        }
        hjMap.put("jknum", jknum);
        hjMap.put("sfnum", sfnum);
        hjMap.put("jynum", jynum);
        hjMap.put("pgnum", pgnum);
        hjMap.put("num", num);
        resultList.add(hjMap);
        return resultList;
    }


    public List<Map<String, Object>> getGZLTJList(String queryDay, String hosnum) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> resultList = followupRecordMapper.getGZLTJList(beginDate, endDate, hosnum);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("doctorName", "合计");
        int rc = 0;
        int rs = 0;

        for (int i = 0; i < resultList.size(); i++) {
            int thenum = 0;
            rc = rc + Integer.parseInt(resultList.get(i).get("rc") + "");
            rs = rs + Integer.parseInt(resultList.get(i).get("rs") + "");
        }
        hjMap.put("rs", rs);
        hjMap.put("rc", rc);

        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getFXZSList(String queryDay, String hosnum) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> resultList = followupRecordMapper.getFXZSList(beginDate, endDate, hosnum);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("doctorName", "合计");
// 心梗
        int xg1 = 0;
        int xg2 = 0;
        int xg3 = 0;
// 血糖
        int xt1 = 0;
        int xt2 = 0;
        int xt3 = 0;
// 疲劳
        int pl1 = 0;
        int pl2 = 0;
        int pl3 = 0;
// 抑郁
        int yiyu1 = 0;
        int yiyu2 = 0;
        int yiyu3 = 0;
// 咽炎
        int yanyan1 = 0;
        int yanyan2 = 0;
        int yanyan3 = 0;
// 胃炎
        int wy1 = 0;
        int wy2 = 0;
        int wy3 = 0;
// 颈椎病
        int jzb1 = 0;
        int jzb2 = 0;
        int jzb3 = 0;
// 脊柱不正
        int jzbz1 = 0;
        int jzbz2 = 0;
        int jzbz3 = 0;
// 血压
        int xy1 = 0;
        int xy2 = 0;
        int xy3 = 0;
// 血脂
        int xz1 = 0;
        int xz2 = 0;
        int xz3 = 0;
// 焦虑
        int jl1 = 0;
        int jl2 = 0;
        int jl3 = 0;
// 睡眠
        int sm1 = 0;
        int sm2 = 0;
        int sm3 = 0;
// 免疫力
        int myl1 = 0;
        int myl2 = 0;
        int myl3 = 0;
// 胃动力不足
        int wdlbz1 = 0;
        int wdlbz2 = 0;
        int wdlbz3 = 0;
// 脑供血不足
        int ngxbz1 = 0;
        int ngxbz2 = 0;
        int ngxbz3 = 0;
// 肾功能减退
        int sgnjt1 = 0;
        int sgnjt2 = 0;
        int sgnjt3 = 0;


        for (int i = 0; i < resultList.size(); i++) {

            xg1 = xg1 + Integer.parseInt(resultList.get(i).get("xg1") + "");
            xg2 = xg2 + Integer.parseInt(resultList.get(i).get("xg2") + "");
            xg3 = xg3 + Integer.parseInt(resultList.get(i).get("xg3") + "");

            xt1 = xt1 + Integer.parseInt(resultList.get(i).get("xt1") + "");
            xt2 = xt2 + Integer.parseInt(resultList.get(i).get("xt2") + "");
            xt3 = xt3 + Integer.parseInt(resultList.get(i).get("xt3") + "");

            pl1 = pl1 + Integer.parseInt(resultList.get(i).get("pl1") + "");
            pl2 = pl2 + Integer.parseInt(resultList.get(i).get("pl2") + "");
            pl3 = pl3 + Integer.parseInt(resultList.get(i).get("pl3") + "");

            yiyu1 = yiyu1 + Integer.parseInt(resultList.get(i).get("yiyu1") + "");
            yiyu2 = yiyu2 + Integer.parseInt(resultList.get(i).get("yiyu2") + "");
            yiyu3 = yiyu3 + Integer.parseInt(resultList.get(i).get("yiyu3") + "");

            yanyan1 = yanyan1 + Integer.parseInt(resultList.get(i).get("yanyan1") + "");
            yanyan2 = yanyan2 + Integer.parseInt(resultList.get(i).get("yanyan2") + "");
            yanyan3 = yanyan3 + Integer.parseInt(resultList.get(i).get("yanyan3") + "");

            wy1 = wy1 + Integer.parseInt(resultList.get(i).get("wy1") + "");
            wy2 = wy2 + Integer.parseInt(resultList.get(i).get("wy2") + "");
            wy3 = wy3 + Integer.parseInt(resultList.get(i).get("wy3") + "");

            jzb1 = jzb1 + Integer.parseInt(resultList.get(i).get("jzb1") + "");
            jzb2 = jzb2 + Integer.parseInt(resultList.get(i).get("jzb2") + "");
            jzb3 = jzb3 + Integer.parseInt(resultList.get(i).get("jzb3") + "");

            jzbz1 = jzbz1 + Integer.parseInt(resultList.get(i).get("jzbz1") + "");
            jzbz2 = jzbz2 + Integer.parseInt(resultList.get(i).get("jzbz2") + "");
            jzbz3 = jzbz3 + Integer.parseInt(resultList.get(i).get("jzbz3") + "");

            xy1 = xy1 + Integer.parseInt(resultList.get(i).get("xy1") + "");
            xy2 = xy2 + Integer.parseInt(resultList.get(i).get("xy2") + "");
            xy3 = xy3 + Integer.parseInt(resultList.get(i).get("xy3") + "");

            xz1 = xz1 + Integer.parseInt(resultList.get(i).get("xz1") + "");
            xz2 = xz2 + Integer.parseInt(resultList.get(i).get("xz2") + "");
            xz3 = xz3 + Integer.parseInt(resultList.get(i).get("xz3") + "");

            jl1 = jl1 + Integer.parseInt(resultList.get(i).get("jl1") + "");
            jl2 = jl2 + Integer.parseInt(resultList.get(i).get("jl2") + "");
            jl3 = jl3 + Integer.parseInt(resultList.get(i).get("jl3") + "");

            sm1 = sm1 + Integer.parseInt(resultList.get(i).get("sm1") + "");
            sm2 = sm2 + Integer.parseInt(resultList.get(i).get("sm2") + "");
            sm3 = sm3 + Integer.parseInt(resultList.get(i).get("sm3") + "");

            myl1 = myl1 + Integer.parseInt(resultList.get(i).get("myl1") + "");
            myl2 = myl2 + Integer.parseInt(resultList.get(i).get("myl2") + "");
            myl3 = myl3 + Integer.parseInt(resultList.get(i).get("myl3") + "");

            wdlbz1 = wdlbz1 + Integer.parseInt(resultList.get(i).get("wdlbz1") + "");
            wdlbz2 = wdlbz2 + Integer.parseInt(resultList.get(i).get("wdlbz2") + "");
            wdlbz3 = wdlbz3 + Integer.parseInt(resultList.get(i).get("wdlbz3") + "");

            ngxbz1 = ngxbz1 + Integer.parseInt(resultList.get(i).get("ngxbz1") + "");
            ngxbz2 = ngxbz2 + Integer.parseInt(resultList.get(i).get("ngxbz2") + "");
            ngxbz3 = ngxbz3 + Integer.parseInt(resultList.get(i).get("ngxbz3") + "");

            sgnjt1 = sgnjt1 + Integer.parseInt(resultList.get(i).get("sgnjt1") + "");
            sgnjt2 = sgnjt2 + Integer.parseInt(resultList.get(i).get("sgnjt2") + "");
            sgnjt3 = sgnjt3 + Integer.parseInt(resultList.get(i).get("sgnjt3") + "");

        }

        hjMap.put("xg1", xg1);
        hjMap.put("xg2", xg2);
        hjMap.put("xg3", xg3);

        hjMap.put("xt1", xt1);
        hjMap.put("xt2", xt2);
        hjMap.put("xt3", xt3);

        hjMap.put("pl1", pl1);
        hjMap.put("pl2", pl2);
        hjMap.put("pl3", pl3);

        hjMap.put("yiyu1", yiyu1);
        hjMap.put("yiyu2", yiyu2);
        hjMap.put("yiyu3", yiyu3);

        hjMap.put("yanyan1", yanyan1);
        hjMap.put("yanyan2", yanyan2);
        hjMap.put("yanyan3", yanyan3);

        hjMap.put("wy1", wy1);
        hjMap.put("wy2", wy2);
        hjMap.put("wy3", wy3);

        hjMap.put("jzb1", jzb1);
        hjMap.put("jzb2", jzb2);
        hjMap.put("jzb3", jzb3);

        hjMap.put("jzbz1", jzbz1);
        hjMap.put("jzbz2", jzbz2);
        hjMap.put("jzbz3", jzbz3);

        hjMap.put("xy1", xy1);
        hjMap.put("xy2", xy2);
        hjMap.put("xy3", xy3);

        hjMap.put("xz1", xz1);
        hjMap.put("xz2", xz2);
        hjMap.put("xz3", xz3);

        hjMap.put("jl1", jl1);
        hjMap.put("jl2", jl2);
        hjMap.put("jl3", jl3);

        hjMap.put("sm1", sm1);
        hjMap.put("sm2", sm2);
        hjMap.put("sm3", sm3);

        hjMap.put("myl1", myl1);
        hjMap.put("myl2", myl2);
        hjMap.put("myl3", myl3);

        hjMap.put("wdlbz1", wdlbz1);
        hjMap.put("wdlbz2", wdlbz2);
        hjMap.put("wdlbz3", wdlbz3);

        hjMap.put("ngxbz1", ngxbz1);
        hjMap.put("ngxbz2", ngxbz2);
        hjMap.put("ngxbz3", ngxbz3);

        hjMap.put("sgnjt1", sgnjt1);
        hjMap.put("sgnjt2", sgnjt2);
        hjMap.put("sgnjt3", sgnjt3);

        resultList.add(hjMap);
        return resultList;
    }

    public Object getZFBZList(String queryDay, String hosnum) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> resultList = followupRecordMapper.getZFBZList(beginDate, endDate, hosnum);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("username", "合计");
        int xsbj = 0; // 心肾不交
        int syangx = 0; // 肾阳虚
        int syinx = 0; // 肾阴虚
        int pw = 0; // 脾胃不调，胃火偏旺
        int srkp = 0; // 湿热困脾
        int shkp = 0; // 寒湿困脾
        int xxyz = 0; // 心血瘀阻
        int ff = 0; // 肺失宣泄，肺气不足
        int xqbz = 0; // 心气不足
        int xhsy = 0; // 心火上炎
        int gg = 0; // 肝气郁结，肝血亏虚
        int qxsk = 0; // 气血双虚
        int xxkx = 0; // 心血亏虚
        int ghsy = 0; // 肝火上炎
        int gxkx = 0; // 肝血亏虚
        int gqyj = 0; // 肝气郁结
        int wyx = 0; // 胃阴虚
        int wqbz = 0; // 胃气不足
        int dhw = 0; // 胆火旺
        int dqx = 0; // 胆气虚
        int pqx = 0; // 脾气虚
        int lh = 0; // 脏腑功能良好
        for (int i = 0; i < resultList.size(); i++) {
            xsbj = xsbj + Integer.parseInt(resultList.get(i).get("心肾不交") + "");
            syangx = syangx + Integer.parseInt(resultList.get(i).get("肾阳虚") + "");
            syinx = syinx + Integer.parseInt(resultList.get(i).get("肾阴虚") + "");
            pw = pw + Integer.parseInt(resultList.get(i).get("脾胃不调，胃火偏旺") + "");
            srkp = srkp + Integer.parseInt(resultList.get(i).get("湿热困脾") + "");
            shkp = shkp + Integer.parseInt(resultList.get(i).get("寒湿困脾") + "");
            xxyz = xxyz + Integer.parseInt(resultList.get(i).get("心血瘀阻") + "");
            ff = ff + Integer.parseInt(resultList.get(i).get("肺失宣泄，肺气不足") + "");
            xqbz = xqbz + Integer.parseInt(resultList.get(i).get("心气不足") + "");
            xhsy = xhsy + Integer.parseInt(resultList.get(i).get("心火上炎") + "");
            gg = gg + Integer.parseInt(resultList.get(i).get("肝气郁结，肝血亏虚") + "");
            qxsk = qxsk + Integer.parseInt(resultList.get(i).get("气血双虚") + "");
            xxkx = xxkx + Integer.parseInt(resultList.get(i).get("心血亏虚") + "");
            ghsy = ghsy + Integer.parseInt(resultList.get(i).get("肝火上炎") + "");
            gxkx = gxkx + Integer.parseInt(resultList.get(i).get("肝血亏虚") + "");
            gqyj = gqyj + Integer.parseInt(resultList.get(i).get("肝气郁结") + "");
            wyx = wyx + Integer.parseInt(resultList.get(i).get("胃阴虚") + "");
            wqbz = wqbz + Integer.parseInt(resultList.get(i).get("胃气不足") + "");
            dhw = dhw + Integer.parseInt(resultList.get(i).get("胆火旺") + "");
            dqx = dqx + Integer.parseInt(resultList.get(i).get("胆气虚") + "");
            pqx = pqx + Integer.parseInt(resultList.get(i).get("脾气虚") + "");
            lh = lh + Integer.parseInt(resultList.get(i).get("脏腑功能良好") + "");
        }
        hjMap.put("心肾不交", xsbj);
        hjMap.put("肾阳虚", syangx);
        hjMap.put("肾阴虚", syinx);
        hjMap.put("脾胃不调，胃火偏旺", pw);
        hjMap.put("湿热困脾", srkp);
        hjMap.put("寒湿困脾", shkp);
        hjMap.put("心血瘀阻", xxyz);
        hjMap.put("肺失宣泄，肺气不足", ff);
        hjMap.put("心气不足", xqbz);
        hjMap.put("心火上炎", xhsy);
        hjMap.put("肝气郁结，肝血亏虚", gg);
        hjMap.put("气血双虚", qxsk);
        hjMap.put("心血亏虚", xxkx);
        hjMap.put("肝火上炎", ghsy);
        hjMap.put("肝血亏虚", gxkx);
        hjMap.put("肝气郁结", gqyj);
        hjMap.put("胃阴虚", wyx);
        hjMap.put("胃气不足", wqbz);
        hjMap.put("胆火旺", dhw);
        hjMap.put("胆气虚", dqx);
        hjMap.put("脾气虚", pqx);
        hjMap.put("脏腑功能良好", lh);
        resultList.add(hjMap);
        Map<String, Map<String, Object>> returnMap = new HashMap();
        for (String key : hjMap.keySet()) {
            System.out.println("key= " + key + " and value= " + hjMap.get(key));
            Map<String, Object> m = new LinkedHashMap<>();
            int heji_index = 0;
            for (int j = 0; j < resultList.size(); j++) {
                if (!"合计".equals(resultList.get(j).get("username") + "")) {
                    m.put(resultList.get(j).get("username") + "", resultList.get(j).get(key));
                } else {
                    heji_index = j;
                }
            }
            m.put("合计", resultList.get(heji_index).get(key));
            returnMap.put(key, m);
//            returnList.add(m);
        }
        returnMap.remove("username");
        System.out.println("===== " + returnMap);
        return returnMap;
    }

    public List<Map<String, Object>> getTZXXList(String queryDay, String hosnum) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> resultList = followupRecordMapper.getTZXXList(beginDate, endDate, hosnum);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("username", "合计");
        int yin1 = 0;
        int yin2 = 0;
        int yin3 = 0;
        int yin4 = 0;
        int yang1 = 0;
        int yang2 = 0;
        int yang3 = 0;
        int yang4 = 0;
        int xueyu1 = 0;
        int xueyu2 = 0;
        int xueyu3 = 0;
        int shiqi1 = 0;
        int shiqi2 = 0;
        int shiqi3 = 0;
        for (int i = 0; i < resultList.size(); i++) {
//			int thenum=0;
            yin1 = yin1 + Integer.parseInt(resultList.get(i).get("yin1") + "");
            yin2 = yin2 + Integer.parseInt(resultList.get(i).get("yin2") + "");
            yin3 = yin3 + Integer.parseInt(resultList.get(i).get("yin3") + "");
            yin4 = yin4 + Integer.parseInt(resultList.get(i).get("yin4") + "");
            yang1 = yang1 + Integer.parseInt(resultList.get(i).get("yang1") + "");
            yang2 = yang2 + Integer.parseInt(resultList.get(i).get("yang2") + "");
            yang3 = yang3 + Integer.parseInt(resultList.get(i).get("yang3") + "");
            yang4 = yang4 + Integer.parseInt(resultList.get(i).get("yang4") + "");
            xueyu1 = xueyu1 + Integer.parseInt(resultList.get(i).get("xueyu1") + "");
            xueyu2 = xueyu2 + Integer.parseInt(resultList.get(i).get("xueyu2") + "");
            xueyu3 = xueyu3 + Integer.parseInt(resultList.get(i).get("xueyu3") + "");
            shiqi1 = shiqi1 + Integer.parseInt(resultList.get(i).get("shiqi1") + "");
            shiqi2 = shiqi2 + Integer.parseInt(resultList.get(i).get("shiqi2") + "");
            shiqi3 = shiqi3 + Integer.parseInt(resultList.get(i).get("shiqi3") + "");
        }
        hjMap.put("yin1", yin1);
        hjMap.put("yin2", yin2);
        hjMap.put("yin3", yin3);
        hjMap.put("yin4", yin4);
        hjMap.put("yang1", yang1);
        hjMap.put("yang2", yang2);
        hjMap.put("yang3", yang3);
        hjMap.put("yang4", yang4);
        hjMap.put("xueyu1", xueyu1);
        hjMap.put("xueyu2", xueyu2);
        hjMap.put("xueyu3", xueyu3);
        hjMap.put("shiqi1", shiqi1);
        hjMap.put("shiqi2", shiqi2);
        hjMap.put("shiqi3", shiqi3);
        resultList.add(hjMap);
        return resultList;
    }

    public List<Map<String, Object>> getTZFX(String queryDay, String hosnum, String userid) {
        String beginDate = queryDay.split("~")[0];
        String endDate = queryDay.split("~")[1];
        List<Map<String, Object>> mbClassificationInfoList = followupRecordMapper.getMbClassificationInfoList();
        StringBuffer sql_mb = new StringBuffer();
        StringBuffer sql_select_mb = new StringBuffer();
        for (Map<String, Object> mbClassificationInfo : mbClassificationInfoList) {
            sql_select_mb.append("IFNULL(b." + mbClassificationInfo.get("mb_code") + ", 0) AS " + mbClassificationInfo.get("mb_code") + ",");
            String[] mbInfoArr = (mbClassificationInfo.get("mb_info") + "").split("\\|");
            StringBuffer mbInfoStr = new StringBuffer();
            for (String mbInfo : mbInfoArr) {
                mbInfoStr.append("OR mp.chronic LIKE '%" + mbInfo + "%'");
            }
            if (StringUtils.isNotEmpty(mbInfoStr)){
                mbInfoStr = new StringBuffer(mbInfoStr.substring(2));
            }
            sql_mb.append("count( DISTINCT " +
                    "  CASE " +
                    "  WHEN " + mbInfoStr + " THEN " +
                    "  r1.customerid " +
                    "  ELSE " +
                    "  null " +
                    "  END " +
                    "  ) " + mbClassificationInfo.get("mb_code") + ",");
        }
        List<Map<String, Object>> list = followupRecordMapper.getTZFX(beginDate, endDate, hosnum, userid, sql_mb.toString(), sql_select_mb.toString());
        // TODO: 此处差一个合计计算
        int peerNum = 0; //人数
        int xnxg = 0; // 心脑血管
        int gxy = 0; // 高血压
        int tnb = 0; // 糖尿病
        int zl = 0; // 肿瘤
        int jsws = 0; // 精神卫生
        int qt = 0; // 其他
        int nanNum = 0; // 男性
        int nvNum = 0; // 女性
        int agelt50 = 0; // 年龄段 低于50
        int age5060 = 0; // 年龄段 50-60
        int age6170 = 0; // 年龄段 61-70
        int age7180 = 0; // 年龄段 71-80
        int agegt80 = 0; // 年龄段 高于
        for (Map<String, Object> map : list) {
            peerNum += Integer.parseInt(map.get("peerNum") + "");
            xnxg += Integer.parseInt(map.get("xnxg") + "");
            gxy += Integer.parseInt(map.get("gxy") + "");
            tnb += Integer.parseInt(map.get("tnb") + "");
            zl += Integer.parseInt(map.get("zl") + "");
            jsws += Integer.parseInt(map.get("jsws") + "");
            qt += Integer.parseInt(map.get("qt") + "");
            nanNum += Integer.parseInt(map.get("nanNum") + "");
            nvNum += Integer.parseInt(map.get("nvNum") + "");
            agelt50 += Integer.parseInt(map.get("agelt50") + "");
            age5060 += Integer.parseInt(map.get("age5060") + "");
            age6170 += Integer.parseInt(map.get("age6170") + "");
            age7180 += Integer.parseInt(map.get("age7180") + "");
            agegt80 += Integer.parseInt(map.get("agegt80") + "");
        }
        Map<String, Object> mapHJ = new HashMap<>();
        mapHJ.put("tizhi", "合计");
        mapHJ.put("peerNum", peerNum);
        mapHJ.put("xnxg", xnxg);
        mapHJ.put("gxy", gxy);
        mapHJ.put("tnb", tnb);
        mapHJ.put("zl", zl);
        mapHJ.put("jsws", jsws);
        mapHJ.put("qt", qt);
        mapHJ.put("nanNum", nanNum);
        mapHJ.put("nvNum", nvNum);
        mapHJ.put("agelt50", agelt50);
        mapHJ.put("age5060", age5060);
        mapHJ.put("age6170", age6170);
        mapHJ.put("age7180", age7180);
        mapHJ.put("agegt80", agegt80);
        list.add(mapHJ);
        return list;

    }

    public List<Map<String, Object>> getMbydgzltjbByYear(String queryYear, String hosnum, String userid) {
        List<Map<String, Object>> resultList = followupRecordMapper.getMbydgzltjbByYear(queryYear, hosnum, userid);
        Map<String, Object> hjMap = new HashMap<String, Object>();
        hjMap.put("hosname", "合计");
        hjMap.put("doctorName", "");
        int January = 0;
        int February = 0;
        int March = 0;
        int April = 0;
        int May = 0;
        int June = 0;
        int July = 0;
        int August = 0;
        int September = 0;
        int October = 0;
        int November = 0;
        int December = 0;
        int Total = 0;
        for (int i = 0; i < resultList.size(); i++) {
            January = January + Integer.parseInt(resultList.get(i).get("January") + "");
            February = February + Integer.parseInt(resultList.get(i).get("February") + "");
            March = March + Integer.parseInt(resultList.get(i).get("March") + "");
            April = April + Integer.parseInt(resultList.get(i).get("April") + "");
            May = May + Integer.parseInt(resultList.get(i).get("May") + "");
            June = June + Integer.parseInt(resultList.get(i).get("June") + "");
            July = July + Integer.parseInt(resultList.get(i).get("July") + "");
            August = August + Integer.parseInt(resultList.get(i).get("August") + "");
            September = September + Integer.parseInt(resultList.get(i).get("September") + "");
            October = October + Integer.parseInt(resultList.get(i).get("October") + "");
            November = November + Integer.parseInt(resultList.get(i).get("November") + "");
            December = December + Integer.parseInt(resultList.get(i).get("December") + "");
            Total = Total + Integer.parseInt(resultList.get(i).get("Total") + "");
        }
        hjMap.put("January", January);
        hjMap.put("February", February);
        hjMap.put("March", March);
        hjMap.put("April", April);
        hjMap.put("May", May);
        hjMap.put("June", June);
        hjMap.put("July", July);
        hjMap.put("August", August);
        hjMap.put("September", September);
        hjMap.put("October", October);
        hjMap.put("November", November);
        hjMap.put("December", December);
        hjMap.put("Total", Total);
        resultList.add(hjMap);
        return resultList;
    }

    /**
     * 左侧树
     *
     * @param hosnum
     * @return
     */
    public List<Map<String, Object>> geteCunRenShu(String hosnum) {
        return followupRecordMapper.geteCunRenShu(hosnum);
    }


}
